#include "image_viewer.h"

Etk_Widget *image;

static void _win_resized_cb(Etk_Window *win, void *data);

void show_image(char *filename) {
    static Etk_Widget *win= NULL;
    Etk_Widget *vbox;
    int w, h;
    double ratio;

    win = etk_window_new();
    etk_window_title_set(ETK_WINDOW(win), "E-Mail ImageViewer, scaled");
    etk_signal_connect("delete-event", ETK_OBJECT(win), ETK_CALLBACK(etk_window_hide_on_delete), NULL);
    etk_signal_connect("resized", ETK_OBJECT(win), ETK_CALLBACK(_win_resized_cb), NULL);

    vbox = etk_vbox_new(ETK_TRUE, 0);
    etk_container_add(ETK_CONTAINER(win), vbox);

    image = etk_image_new_from_file(filename, NULL);
    etk_image_keep_aspect_set(ETK_IMAGE(image), ETK_TRUE);
    etk_container_add(ETK_CONTAINER(vbox), image);
    etk_image_size_get(ETK_IMAGE(image), &w, &h);
    ratio = (double)w / h;
    if (w > DEF_W) {
        etk_widget_size_request_set(ETK_WIDGET(image), DEF_W, (int)(DEF_W / ratio));
        etk_window_resize(ETK_WINDOW(win), DEF_W, (int)(DEF_W / ratio));
    }
    etk_widget_show_all(win);
}

static void _win_resized_cb(Etk_Window *win, void *data) {
    int x, y, w, h;

    etk_window_geometry_get(ETK_WINDOW(win), &x, &y, &w, &h);
    etk_widget_size_request_set(ETK_WIDGET(image), w - 5, h - 5);
}
